package com.dhl.cloud.security;

import java.util.Date;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.exceptions.JWTDecodeException;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.dhl.cloud.common.Constants;

/**
 * @desc JWT工具类
 **/
public class JwtUtil {
 
//    private static final long EXPIRE_TIME = 5 * 60 * 1000;
    private static final String SECRET = "dhl";
    
    /**
     * 校验token是否正确
     *
     * @param token  密钥
     * @param secret 用户的密码
     * @return 是否正确
     */
    public static boolean verify(String token, String username) {
        try {
            //根据密码生成JWT效验器
            Algorithm algorithm = Algorithm.HMAC256(SECRET);
            JWTVerifier verifier = JWT.require(algorithm)
                    .withClaim("username", username)
                    .build();
            //效验TOKEN
            DecodedJWT jwt = verifier.verify(token);
            return true;
        } catch (Exception exception) {
            return false;
        }
    }
 
    public static boolean verify(String token) {
        try {
            //根据密码生成JWT效验器
            Algorithm algorithm = Algorithm.HMAC256(SECRET);
            JWTVerifier verifier = JWT.require(algorithm)
                    .build();
            //效验TOKEN
            DecodedJWT jwt = verifier.verify(token);
            return true;
        } catch (Exception exception) {
            return false;
        }
    }
    
    /**
     * 获得token中的信息无需secret解密也能获得
     *
     * @return token中包含的用户名
     */
    public static String getUsername(String token) {
        try {
            DecodedJWT jwt = JWT.decode(token);
            return jwt.getClaim("username").asString();
        } catch (JWTDecodeException e) {
            return null;
        }
    }
 
    /**
     * 获得token中的信息无需secret解密也能获得
     *
     * @return token中包含的用户租户ID
     */
    public static String getTenantid(String token) {
        try {
            DecodedJWT jwt = JWT.decode(token);
            return jwt.getClaim("tenantid").asString();
        } catch (JWTDecodeException e) {
            return null;
        }
    }
    
    /**
     * 生成签名,5min后过期
     *
     * @param username 用户名
     * @param tenantid 用户租户ID
     * @return 加密的token
     */
//    public static String sign(String username,String tenantid) {
////        Date date = new Date(System.currentTimeMillis() + EXPIRE_TIME);
//        Algorithm algorithm = Algorithm.HMAC256(SECRET);
//        // 附带username信息
//        return JWT.create()
//                .withClaim("username", username)
//                .withClaim("tenantid", tenantid)
////                .withExpiresAt(date)
//                .sign(algorithm);
// 
//    }
    

    /**
     * 生成签名,n分钟后过期
     * @param
     * @param currentTimeMillis
     * @return
     */
    public static String sign(String username,String tenantid, String currentTimeMillis,String tokenExpireTime) {
        //过期时间，单位：毫秒
        Date date = new Date(System.currentTimeMillis() + Integer.parseInt(tokenExpireTime)*60*1000L);
        Algorithm algorithm = Algorithm.HMAC256(SECRET);

        return JWT.create()
        		.withClaim("username", username)
                .withClaim("tenantid", tenantid)
                .withClaim(Constants.CURRENT_TIME_MILLIS, currentTimeMillis)
                .withExpiresAt(date)
                .sign(algorithm);
    }
    
    /**
     * 获得Token中的信息无需secret解密也能获得
     * @param token
     * @param claim
     * @return
     */
    public static String getClaim(String token, String claim) {
        try {
            DecodedJWT jwt = JWT.decode(token);
            return jwt.getClaim(claim).asString();
        } catch (JWTDecodeException e) {
            return null;
        }
    }
}